from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm, PasswordChangeForm
from .models import CustomUser
from captcha.fields import CaptchaField


class CustomUserCreationForm(UserCreationForm):
    captcha = CaptchaField(label='验证码')

    class Meta:
        model = CustomUser
        fields = ('username', 'email', 'phone', 'avatar', 'location', 'bio')
        widgets = {
            'username': forms.TextInput(attrs={'placeholder': '请输入用户名'}),
            'email': forms.EmailInput(attrs={'placeholder': '请输入邮箱'}),
            'phone': forms.TextInput(attrs={'placeholder': '请输入手机号'}),
            'location': forms.TextInput(attrs={'placeholder': '请输入所在地'}),
            'bio': forms.Textarea(attrs={
                'placeholder': '简单介绍一下自己...',
                'rows': 3
            }),
        }


class CustomUserChangeForm(UserChangeForm):
    class Meta:
        model = CustomUser
        fields = ('username', 'email', 'phone', 'avatar', 'bio', 'location')


class LoginForm(forms.Form):
    username = forms.CharField(
        max_length=150,
        label='用户名',
        widget=forms.TextInput(attrs={
            'placeholder': '请输入用户名',
            'class': 'form-control'
        })
    )
    password = forms.CharField(
        widget=forms.PasswordInput(attrs={
            'placeholder': '请输入密码',
            'class': 'form-control'
        }),
        label='密码'
    )
    captcha = CaptchaField(label='验证码')


class ProfileUpdateForm(forms.ModelForm):
    """个人资料更新表单"""

    class Meta:
        model = CustomUser
        fields = ['username', 'email', 'phone', 'bio', 'location']
        widgets = {
            'bio': forms.Textarea(attrs={'rows': 4, 'placeholder': '介绍一下自己...'}),
        }


class AvatarUpdateForm(forms.ModelForm):
    """头像更新表单"""

    class Meta:
        model = CustomUser
        fields = ['avatar']
        labels = {
            'avatar': '选择新头像',
        }


class PasswordChangeForm(PasswordChangeForm):
    """密码修改表单"""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields:
            self.fields[field].widget.attrs.update({'class': 'form-control'})


from django import forms
from django.contrib.auth.forms import PasswordChangeForm
from .models import CustomUser


class UserProfileForm(forms.ModelForm):
    """用户个人资料表单"""

    class Meta:
        model = CustomUser
        fields = ['first_name', 'last_name', 'email', 'phone', 'bio', 'location', 'avatar']
        widgets = {
            'first_name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入名字'
            }),
            'last_name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入姓氏'
            }),
            'email': forms.EmailInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入邮箱地址'
            }),
            'phone': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入手机号'
            }),
            'bio': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 4,
                'placeholder': '介绍一下您自己...'
            }),
            'location': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': '请输入所在地'
            }),
        }
        labels = {
            'first_name': '名字',
            'last_name': '姓氏',
            'email': '邮箱',
            'phone': '手机号',
            'bio': '个人简介',
            'location': '所在地',
            'avatar': '头像',
        }


class AvatarUpdateForm(forms.ModelForm):
    """头像更新表单"""

    class Meta:
        model = CustomUser
        fields = ['avatar']
        widgets = {
            'avatar': forms.FileInput(attrs={
                'class': 'form-control',
                'accept': 'image/*'
            })
        }
        labels = {
            'avatar': '选择头像'
        }


class CustomPasswordChangeForm(PasswordChangeForm):
    """自定义密码修改表单"""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # 为所有字段添加form-control类
        for field_name in self.fields:
            self.fields[field_name].widget.attrs.update({
                'class': 'form-control'
            })

